﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Web;


namespace YSC_Backstage_Web.RestClient
{
    public class LoginFilterAttribute : ActionFilterAttribute
    {

        /// <summary>
        /// 改写onactionexecuting(在controller action执行之前调用)，去判断请求中是不是存了session。使用场景：如何验证登录等。
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (context.Filters.Any(item => item is IAllowAnonymousFilter))
            {
                return;
            }

            if (!(context.ActionDescriptor is ControllerActionDescriptor))
            {
                return;
            }
            var attributeList = new List<object>();
            attributeList.AddRange((context.ActionDescriptor as ControllerActionDescriptor).MethodInfo.GetCustomAttributes(true));
            attributeList.AddRange((context.ActionDescriptor as ControllerActionDescriptor).MethodInfo.DeclaringType.GetCustomAttributes(true));
            var authorizeAttributes = attributeList.OfType<ILoginFilterAttribute>().ToList();
            var claims = context.HttpContext.User.Claims;

            var claimsAll = context.HttpContext.User.Claims.Select(p => new { p.Type, p.Value });

            var items = claimsAll.FirstOrDefault(p => p.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");

            if (items == null)
            {
                context.HttpContext.Response.Redirect("/Login/Login");
                context.Result = new EmptyResult();
            }
        }

        //public void OnAuthorization(AuthorizationFilterContext context)
        //{
        //    if (context.Filters.Any(item => item is IAllowAnonymousFilter))
        //    {
        //        return;
        //    }

        //    if (!(context.ActionDescriptor is ControllerActionDescriptor))
        //    {
        //        return;
        //    }
        //    var attributeList = new List<object>();
        //    attributeList.AddRange((context.ActionDescriptor as ControllerActionDescriptor).MethodInfo.GetCustomAttributes(true));
        //    attributeList.AddRange((context.ActionDescriptor as ControllerActionDescriptor).MethodInfo.DeclaringType.GetCustomAttributes(true));
        //    var authorizeAttributes = attributeList.OfType<ILoginFilterAttribute>().ToList();
        //    var claims = context.HttpContext.User.Claims;

        //    var claimsAll = context.HttpContext.User.Claims.Select(p => new { p.Type, p.Value });

        //    var items = claimsAll.FirstOrDefault(p => p.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");

        //    var userPermissions = "User_MVC";

        //    if (!authorizeAttributes.Any(s => s.Permission.Equals(userPermissions)) && items == null && authorizeAttributes.Count() > 0)
        //    {
        //        context.HttpContext.Response.WriteAsync("<script>window.parent.location.href='/Login/Login'</script>");
        //    }
        //    return;                   
        //}

    }
}
